package android.academy.haixue.com.haixue.activity;

import android.academy.haixue.com.haixue.fragment.BaseFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.ViewGroup;

import org.apache.log4j.Logger;

import butterknife.ButterKnife;
import cn.woblog.android.common.db.DBController;
import cn.woblog.android.common.skin.core.ThemeManager;
import cn.woblog.android.common.utils.SharePreferenceUtils;

/**
 * Created by ren on 2015/4/27 0027.
 */
public class BaseActivity extends FragmentActivity{
    protected final static Logger log = Logger.getLogger(new Exception()
            .getStackTrace()[2].getClassName());
    protected SharePreferenceUtils sp;
    protected DBController dbController;

    /**
     * 找控件
     */
    protected void initViews() {

    }

    /**
     * 动态设置样式，颜色，宽高，背景
     */
    protected void initStyles() {
    }

    /**
     *设置数据
     */
    protected void initDatas() {

    }

    /**
     * 绑定监听器
     */
    protected void initListener() {
    }


    /**通过主题管理器获取对应的颜色
     * @param id
     * @return
     */
    protected int getColor(int id){
        return ThemeManager.getColor(id);
    }

    /**通过主题管理器获取对应的尺寸
     * @param id
     * @return
     */
    protected int getDimen(int id){
        return ThemeManager.getDimen(id);
    }

    /**通过主题管理器获取对应的drawable
     * @param id
     * @return
     */
    protected android.graphics.drawable.Drawable getDrawableById(int id){
        return ThemeManager.getDrawable(id);
    }


    /**替换fragment
     * @param id
     * @param fragment
     * @return
     */
   protected int replaceFragment(int id, BaseFragment fragment){
      return getCustomFragmentManager().beginTransaction().replace(id,fragment).commit();
   }

    /**添加fragment
     * @param id
     * @param fragment
     * @return
     */
    protected int addFragment(int id, BaseFragment fragment){
        return getCustomFragmentManager().beginTransaction().add(id, fragment).commit();
    }

    /**显示一个fragment
     * @param fragment
     * @return
     */
    protected int showFragment(BaseFragment fragment){
        return getCustomFragmentManager().beginTransaction().show(fragment).commit();
    }

    private FragmentManager getCustomFragmentManager() {
        return getSupportFragmentManager();
    }

    @Override
    protected void onResume() {
        if (ThemeManager.isRefreshUI()) {
            initStyles();
        }
        super.onResume();
    }

    private void init() {
        initLog();
        sp = SharePreferenceUtils.getInstance(this.getApplicationContext());
        ButterKnife.inject(this);
        initViews();
        initDB();
        initStyles();
        initDatas();
        initListener();
    }

    private void initLog() {

    }

    protected void initDB() {
        dbController = DBController.getInstance(getApplicationContext());
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        init();
    }
    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        init();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
        init();
    }
}
